/*
 * GestaoMatricular_insDisciplina.java
 *
 * Criado em 26/08/2009, 19:16:56
 */

package siga.visao_academico;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import siga.banco.conexao;
import siga.visao_busca.buscaDisciplina;
import siga.visao_busca.buscaVer;
import siga.visao.index;
import siga.visao.verErros;

/**
 *
 * @author rafaelfrasson
 */
public class GestaoMatricular_insDisciplina extends javax.swing.JDialog {
    public boolean disciplia=false;
    String ano,semestre;
    public GestaoMatricular_insDisciplina(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfCodDisc = new javax.swing.JTextField();
        jtfDesDisc = new javax.swing.JTextField();
        jbBusca2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jtfCodProfT = new javax.swing.JTextField();
        jtfDesProfT = new javax.swing.JTextField();
        jbBusca3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jtfCodProfA = new javax.swing.JTextField();
        jtfDesProfA = new javax.swing.JTextField();
        jbBusca4 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jtfInsTotAcad = new javax.swing.JCheckBox();
        jLabel5 = new javax.swing.JLabel();
        jtfTurma = new javax.swing.JTextField();
        jtfCodAcad = new javax.swing.JTextField();
        jtfDesAcad = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inserindo Disciplina");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Discipina");
        jLabel1.setName("jLabel1"); // NOI18N

        jtfCodDisc.setEditable(false);
        jtfCodDisc.setName("jtfCodDisc"); // NOI18N

        jtfDesDisc.setEditable(false);
        jtfDesDisc.setName("jtfDesDisc"); // NOI18N

        jbBusca2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbBusca2.setText("Buscar");
        jbBusca2.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jbBusca2.setName("jbBusca2"); // NOI18N
        jbBusca2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBusca2ActionPerformed(evt);
            }
        });

        jLabel2.setText("Professor Titular");
        jLabel2.setName("jLabel2"); // NOI18N

        jtfCodProfT.setEditable(false);
        jtfCodProfT.setName("jtfCodProfT"); // NOI18N

        jtfDesProfT.setEditable(false);
        jtfDesProfT.setName("jtfDesProfT"); // NOI18N

        jbBusca3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbBusca3.setText("Buscar");
        jbBusca3.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jbBusca3.setName("jbBusca3"); // NOI18N
        jbBusca3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBusca3ActionPerformed(evt);
            }
        });

        jLabel3.setText("Professor Adjunto");
        jLabel3.setName("jLabel3"); // NOI18N

        jtfCodProfA.setEditable(false);
        jtfCodProfA.setName("jtfCodProfA"); // NOI18N

        jtfDesProfA.setEditable(false);
        jtfDesProfA.setName("jtfDesProfA"); // NOI18N

        jbBusca4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbBusca4.setText("Buscar");
        jbBusca4.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jbBusca4.setName("jbBusca4"); // NOI18N
        jbBusca4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBusca4ActionPerformed(evt);
            }
        });

        jButton1.setText("Salvar");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel4.setText("Inserir esta disciplina para TODOS os Acadêmicos:");
        jLabel4.setName("jLabel4"); // NOI18N

        jtfInsTotAcad.setText("Sim");
        jtfInsTotAcad.setName("jtfInsTotAcad"); // NOI18N

        jLabel5.setText("Turma");
        jLabel5.setName("jLabel5"); // NOI18N

        jtfTurma.setEditable(false);
        jtfTurma.setName("jtfTurma"); // NOI18N

        jtfCodAcad.setEditable(false);
        jtfCodAcad.setName("jtfCodAcad"); // NOI18N

        jtfDesAcad.setEditable(false);
        jtfDesAcad.setName("jtfDesAcad"); // NOI18N

        jLabel6.setText("Acadêmico");
        jLabel6.setName("jLabel6"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel5)
                            .addGap(478, 478, 478))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(95, 95, 95)
                            .addComponent(jLabel6)
                            .addGap(356, 356, 356))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jtfTurma, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtfCodAcad, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtfDesAcad, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtfInsTotAcad)
                            .addContainerGap(235, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jtfCodProfT, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jtfDesProfT, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jbBusca3))
                                .addComponent(jLabel3)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jtfCodProfA, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jtfDesProfA, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jbBusca4))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jtfCodDisc, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jtfDesDisc, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jbBusca2)))
                            .addContainerGap()))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfCodAcad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDesAcad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtfInsTotAcad))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfCodDisc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDesDisc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbBusca2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfCodProfT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDesProfT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbBusca3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfCodProfA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDesProfA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbBusca4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbBusca2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBusca2ActionPerformed
        buscaDisciplina bd = new buscaDisciplina(null,true);
        bd.setLocation((index.tela.width-bd.getWidth())/2, (index.tela.height-bd.getHeight())/2);
        bd.setVisible(true);
        if(!index.LRdisciplina.get(0).equals("")){
            try {
                jtfCodDisc.setText(index.LRdisciplina.get(0).toString());
                jtfDesDisc.setText(index.LRdisciplina.get(1).toString());
                conexao.Query("select p.codigo as cod_t, p.descricao as des_t, p2.codigo as cod_a, p2.descricao as des_a from grade_disciplina gd " + " left join pessoas p on p.codigo=gd.cod_prof_t and p.cod_emp=gd.cod_emp " + " left join pessoas p2 on p2.codigo=gd.cod_prof_a and p2.cod_emp=gd.cod_emp " + " where gd.codigo=" + index.LRdisciplina.get(0) + " and gd.cod_emp=" + index.cod_emp);
                conexao.rs.next();
                jtfCodProfT.setText((conexao.rs.getString("cod_t")==null)?"":conexao.rs.getString("cod_t").toString());
                jtfDesProfT.setText((conexao.rs.getString("des_t")==null)?"":conexao.rs.getString("des_t").toString());
                jtfCodProfA.setText((conexao.rs.getString("cod_a")==null)?"":conexao.rs.getString("cod_a").toString());
                jtfDesProfA.setText((conexao.rs.getString("des_a")==null)?"":conexao.rs.getString("des_a").toString());
                
            } catch (SQLException ex) {
                verErros.setErro(ex.getMessage());
            }
        }
        index.LRdisciplina.add(0,"");
        index.LRdisciplina.add(1,"");
}//GEN-LAST:event_jbBusca2ActionPerformed

    private void jbBusca3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBusca3ActionPerformed
        buscaVer.setTipo("Docente");
        buscaVer bv = new buscaVer(null,true);
        bv.setLocation((index.tela.width-bv.getWidth())/2, (index.tela.height-bv.getHeight())/2);
        bv.setVisible(true);
        if(buscaVer.isConfirmaBusca()){
            jtfCodProfT.setText(index.LRpessoa.get(0).toString());
            jtfDesProfT.setText(index.LRpessoa.get(1).toString());
        }
        index.LRpessoa.add(0, "");
        index.LRpessoa.add(1, "");
    }//GEN-LAST:event_jbBusca3ActionPerformed

    private void jbBusca4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBusca4ActionPerformed
        buscaVer.setTipo("Docente");
        buscaVer bv = new buscaVer(null,true);
        bv.setLocation((index.tela.width-bv.getWidth())/2, (index.tela.height-bv.getHeight())/2);
        bv.setVisible(true);
        if(buscaVer.isConfirmaBusca()){
            jtfCodProfA.setText(index.LRpessoa.get(0).toString());
            jtfDesProfA.setText(index.LRpessoa.get(1).toString());
        }
        index.LRpessoa.add(0, "");
        index.LRpessoa.add(1, "");
    }//GEN-LAST:event_jbBusca4ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {
            jtfTurma.setText(index.LRturma.get(0).toString());
            conexao.Query("select * from grade_turma where codigo=\"" + index.LRturma.get(0) + "\" and cod_fase=" + index.LRfase.get(0) + " and cod_emp=" + index.cod_emp);
            conexao.rs.next();
            ano = conexao.rs.getString("ano").toString();
            semestre = conexao.rs.getString("semestre").toString();
            jtfCodAcad.setText(index.LRpessoa.get(0).toString());
            jtfDesAcad.setText(index.LRpessoa.get(1).toString());
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }//GEN-LAST:event_formWindowOpened

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
       
    }//GEN-LAST:event_formWindowClosed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(!jtfDesDisc.getText().equals("")){
            if(!jtfDesProfT.getText().equals("")){
                String prfa,dprofa;
                if(jtfCodProfA.getText().equals("")){prfa="";dprofa="";}else{prfa="cod_prof_a,";dprofa=""+jtfCodProfA.getText()+",";}
                if(jtfInsTotAcad.isSelected()){
                    try {
                        List cod_pessoa = new ArrayList();
                        conexao.Query("select cod_pessoa from diario where cod_emp=" + index.cod_emp + " and cod_matricula=\"" + jtfTurma.getText() + "\" and ano=" + ano + " and cod_fase=" + index.LRfase.get(0) + " and cod_situacao<>8 group by cod_pessoa");
                        while (conexao.rs.next()) {
                            cod_pessoa.add(conexao.rs.getString("cod_pessoa"));
                        }
                        for(int i=0;i<cod_pessoa.size();i++){
                            conexao.Inserir("replace into diario(cod_pessoa,cod_emp,cod_fase,ano,cod_disciplina,cod_matricula,cod_curso,cod_prof_t,"+prfa+"cod_situacao,semestre,dt_cancelamento) " +
                            "values("+cod_pessoa.get(i)+","+index.cod_emp+","+index.LRfase.get(0)+","+ano+","+jtfCodDisc.getText()+",\""+jtfTurma.getText()+"\","+jtfTurma.getText().substring(0,3)+","+jtfCodProfT.getText()+","+dprofa+"1,\""+ano+"/"+semestre+"\",now())");
                        }
                        JOptionPane.showMessageDialog(null, "Disciplina Inserida para todos os Acadêmicos.","Inserindo Disciplina",JOptionPane.INFORMATION_MESSAGE);
                    } catch (SQLException ex) {
                        verErros.setErro(ex.getMessage());
                    }
                }else{
                    conexao.Inserir("replace into diario(cod_pessoa,cod_emp,cod_fase,ano,cod_disciplina,cod_matricula,cod_curso,cod_prof_t,"+prfa+"cod_situacao,semestre,dt_cancelamento) " +
                            "values("+jtfCodAcad.getText()+","+index.cod_emp+","+index.LRfase.get(0)+","+ano+","+jtfCodDisc.getText()+",\""+jtfTurma.getText()+"\","+jtfTurma.getText().substring(0,3)+","+jtfCodProfT.getText()+","+dprofa+"1,\""+ano+"/"+semestre+"\",now())");
                    JOptionPane.showMessageDialog(null, "Disciplina Inserida.","Inserindo Disciplina",JOptionPane.INFORMATION_MESSAGE);
                }
                disciplia=true;
                dispose();
            }else{
                JOptionPane.showMessageDialog(null, "Campo Professor Titular é obrigatório.","Alerta do Sistema.",JOptionPane.INFORMATION_MESSAGE);
                jbBusca3.requestFocus();
            }
        }else{
            JOptionPane.showMessageDialog(null, "Campo Disciplina é obrigatório.","Alerta do Sistema.",JOptionPane.INFORMATION_MESSAGE);
            jButton2.requestFocus();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        disciplia=true;
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton jbBusca2;
    private javax.swing.JButton jbBusca3;
    private javax.swing.JButton jbBusca4;
    private javax.swing.JTextField jtfCodAcad;
    private javax.swing.JTextField jtfCodDisc;
    private javax.swing.JTextField jtfCodProfA;
    private javax.swing.JTextField jtfCodProfT;
    private javax.swing.JTextField jtfDesAcad;
    private javax.swing.JTextField jtfDesDisc;
    private javax.swing.JTextField jtfDesProfA;
    private javax.swing.JTextField jtfDesProfT;
    private javax.swing.JCheckBox jtfInsTotAcad;
    private javax.swing.JTextField jtfTurma;
    // End of variables declaration//GEN-END:variables

}
